<?php
namespace App\Policies\Admin;

use App\Models\AdminUser;
use App\Models\Department;

class UploadPolicy
{
    public $model;
    protected $admin_user;
    protected $xref_exists = false;
    protected $is_super_manger;
    protected $configs;
    protected $end_time;

    public function __construct(Department $model = null, AdminUser $admin_user = null)
    {
        $this->model = $model;
        $this->admin_user = $admin_user;
        $this->is_super_manger = $admin_user->isSuperManger();
        $this->end_time = "2024-02-01 00:00:00";
    }

    public function unique_condition()
    {
        if ($this->model->type == Department::TYPE_BZ) {
            return $this->admin_user->dwdm == $this->model->dwdm;
        } else if ($this->model->type == Department::TYPE_DJ) {
            return $this->admin_user->ddb_dwdm == $this->model->dwdm;
        }
        return false;
    }

    public function timeValid()
    {
        return now()->toDateTimeString() <= $this->end_time;
    }

    /**
     * 是否可以查看文件
     */
    public function can_view()
    {
        if ($this->is_super_manger) {
            return true;
        }
        return $this->unique_condition();
    }

    /**
     * 是否可以下载
     */
    public function can_download()
    {
        if ($this->is_super_manger) {
            return true;
        }
        return $this->unique_condition();
    }

    /**
     * 是否可以上传文件
     */
    public function can_upload()
    {
        return $this->timeValid() && $this->unique_condition();
    }

    /**
     * 是否可以提交审核
     */
    public function can_submit_verify()
    {
        if (!$this->unique_condition()) {
            return false;
        }
        # 如果已经通过，则不需要再审核
        if ($this->model->status == Department::STATUS_VERIFY_SUCCESS) {
            return false;
        }
        if ($this->model->status == Department::STATUS_VERIFY_PENDING) {
            return false;
        }
        # 判断所有文件是否都已经上传完成
        // $all_ready_errors = $this->model->checkAllFilesReady();
        // if ($all_ready_errors) {
        //     return false;
        // }
        return true;
    }

    public static function make(...$params)
    {
        return new static(...$params);
    }
}